<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('procure.suppliers.supplierid')" type="string" v-model="baTable.form.items!.supplierid" prop="supplierid" :placeholder="t('Please input field', { field: t('procure.suppliers.supplierid') })" />
                    <FormItem :label="t('procure.suppliers.suppname')" type="string" v-model="baTable.form.items!.suppname" prop="suppname" :placeholder="t('Please input field', { field: t('procure.suppliers.suppname') })" />
                    <FormItem :label="t('procure.suppliers.address1')" type="string" v-model="baTable.form.items!.address1" prop="address1" :placeholder="t('Please input field', { field: t('procure.suppliers.address1') })" />
                    <FormItem :label="t('procure.suppliers.address2')" type="string" v-model="baTable.form.items!.address2" prop="address2" :placeholder="t('Please input field', { field: t('procure.suppliers.address2') })" />
                    <FormItem :label="t('procure.suppliers.address3')" type="string" v-model="baTable.form.items!.address3" prop="address3" :placeholder="t('Please input field', { field: t('procure.suppliers.address3') })" />
                    <FormItem :label="t('procure.suppliers.address4')" type="string" v-model="baTable.form.items!.address4" prop="address4" :placeholder="t('Please input field', { field: t('procure.suppliers.address4') })" />
                    <FormItem :label="t('procure.suppliers.address5')" type="string" v-model="baTable.form.items!.address5" prop="address5" :placeholder="t('Please input field', { field: t('procure.suppliers.address5') })" />
                    <FormItem :label="t('procure.suppliers.address6')" type="string" v-model="baTable.form.items!.address6" prop="address6" :placeholder="t('Please input field', { field: t('procure.suppliers.address6') })" />
                    <FormItem :label="t('procure.suppliers.supptype')" type="remoteSelect" v-model="baTable.form.items!.supptype" prop="supptype" :input-attr="{ pk: 'cncerp_procure_suppliertype.typeid', field: 'typename', 'remote-url': '/admin/procure.Suppliertype/index' }" :placeholder="t('Please select field', { field: t('procure.suppliers.supptype') })" />
                    <FormItem :label="t('procure.suppliers.lat')" type="number" prop="lat" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lat" :placeholder="t('Please input field', { field: t('procure.suppliers.lat') })" />
                    <FormItem :label="t('procure.suppliers.lng')" type="number" prop="lng" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lng" :placeholder="t('Please input field', { field: t('procure.suppliers.lng') })" />
                    <FormItem :label="t('procure.suppliers.currcode')" type="remoteSelect" v-model="baTable.form.items!.currcode" prop="currcode" :input-attr="{ pk: 'cncerp_fm_currencies.currabrev', field: 'currency', 'remote-url': '/admin/fm.Currencies/index' }" :placeholder="t('Please select field', { field: t('procure.suppliers.currcode') })" />
                    <FormItem :label="t('procure.suppliers.suppliersince')" type="date" v-model="baTable.form.items!.suppliersince" prop="suppliersince" :placeholder="t('Please select field', { field: t('procure.suppliers.suppliersince') })" />
                    <FormItem :label="t('procure.suppliers.lastpaid')" type="number" prop="lastpaid" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lastpaid" :placeholder="t('Please input field', { field: t('procure.suppliers.lastpaid') })" />
                    <FormItem :label="t('procure.suppliers.lastpaiddate')" type="datetime" v-model="baTable.form.items!.lastpaiddate" prop="lastpaiddate" :placeholder="t('Please select field', { field: t('procure.suppliers.lastpaiddate') })" />
                    <FormItem :label="t('procure.suppliers.paymentterms')" type="remoteSelect" v-model="baTable.form.items!.paymentterms" prop="paymentterms" :input-attr="{ pk: 'cncerp_sales_paymentterms.termsindicator', field: 'terms', 'remote-url': '/admin/sales.Paymentterms/index' }" :placeholder="t('Please select field', { field: t('procure.suppliers.paymentterms') })" />
                    <FormItem :label="t('procure.suppliers.bankact')" type="string" v-model="baTable.form.items!.bankact" prop="bankact" :placeholder="t('Please input field', { field: t('procure.suppliers.bankact') })" />
                    <FormItem :label="t('procure.suppliers.bankref')" type="string" v-model="baTable.form.items!.bankref" prop="bankref" :placeholder="t('Please input field', { field: t('procure.suppliers.bankref') })" />
                    <FormItem :label="t('procure.suppliers.bankpartics')" type="string" v-model="baTable.form.items!.bankpartics" prop="bankpartics" :placeholder="t('Please input field', { field: t('procure.suppliers.bankpartics') })" />
                    <FormItem :label="t('procure.suppliers.remittance')" type="radio" v-model="baTable.form.items!.remittance" prop="remittance" :data="{ content: { '1': t('procure.suppliers.remittance 1'), '0': t('procure.suppliers.remittance 0') } }" :placeholder="t('Please select field', { field: t('procure.suppliers.remittance') })" />
                    <FormItem :label="t('procure.suppliers.taxgroupid')" type="remoteSelect" v-model="baTable.form.items!.taxgroupid" prop="taxgroupid" :input-attr="{ pk: 'cncerp_setup_taxgroups.taxgroupid', field: 'taxgroupdescription', 'remote-url': '/admin/setup.Taxgroups/index' }" :placeholder="t('Please select field', { field: t('procure.suppliers.taxgroupid') })" />
                    <FormItem :label="t('procure.suppliers.factorcompanyid')" type="remoteSelect" v-model="baTable.form.items!.factorcompanyid" prop="factorcompanyid" :input-attr="{ pk: 'cncerp_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('procure.suppliers.factorcompanyid') })" />
                    <FormItem :label="t('procure.suppliers.taxref')" type="string" v-model="baTable.form.items!.taxref" prop="taxref" :placeholder="t('Please input field', { field: t('procure.suppliers.taxref') })" />
                    <FormItem :label="t('procure.suppliers.phn')" type="string" v-model="baTable.form.items!.phn" prop="phn" :placeholder="t('Please input field', { field: t('procure.suppliers.phn') })" />
                    <FormItem :label="t('procure.suppliers.port')" type="string" v-model="baTable.form.items!.port" prop="port" :placeholder="t('Please input field', { field: t('procure.suppliers.port') })" />
                    <FormItem :label="t('procure.suppliers.email')" type="string" v-model="baTable.form.items!.email" prop="email" :placeholder="t('Please input field', { field: t('procure.suppliers.email') })" />
                    <FormItem :label="t('procure.suppliers.fax')" type="string" v-model="baTable.form.items!.fax" prop="fax" :placeholder="t('Please input field', { field: t('procure.suppliers.fax') })" />
                    <FormItem :label="t('procure.suppliers.telephone')" type="string" v-model="baTable.form.items!.telephone" prop="telephone" :placeholder="t('Please input field', { field: t('procure.suppliers.telephone') })" />
                    <FormItem :label="t('procure.suppliers.url')" type="string" v-model="baTable.form.items!.url" prop="url" :placeholder="t('Please input field', { field: t('procure.suppliers.url') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    supplierid: [buildValidatorData({ name: 'required', title: t('procure.suppliers.supplierid') })],
    suppname: [buildValidatorData({ name: 'required', title: t('procure.suppliers.suppname') })],
    supptype: [buildValidatorData({ name: 'required', title: t('procure.suppliers.supptype') })],
    currcode: [buildValidatorData({ name: 'required', title: t('procure.suppliers.currcode') })],
    suppliersince: [buildValidatorData({ name: 'date', title: t('procure.suppliers.suppliersince') })],
    lastpaid: [buildValidatorData({ name: 'number', title: t('procure.suppliers.lastpaid') })],
    lastpaiddate: [buildValidatorData({ name: 'date', title: t('procure.suppliers.lastpaiddate') })],
    remittance: [buildValidatorData({ name: 'required', title: t('procure.suppliers.remittance') })],
    taxgroupid: [buildValidatorData({ name: 'number', title: t('procure.suppliers.taxgroupid') })],
    factorcompanyid: [buildValidatorData({ name: 'number', title: t('procure.suppliers.factorcompanyid') })],
    email: [buildValidatorData({ name: 'email', title: t('procure.suppliers.email') })],
    url: [buildValidatorData({ name: 'url', title: t('procure.suppliers.url') })],
})
</script>

<style scoped lang="scss"></style>
